package com.luoyi.util.ehr;

import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.symmetric.AES;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.URLDecoder;


public class CryptoAesUtils {

    public final static String key = "2024rlGONGXIANG&";
    public final static String iv = "2024rlGONGXIANG&";

    public final static String KEY = "key";
    public final static String IV = "iv";

    public final static String SECRET_KEY = "SECRET_KEY";


    private final static String regex = "#";

    /** AES加解密 */
    protected static AES aes = new AES(Mode.CBC, Padding.PKCS5Padding, key.getBytes(), iv.getBytes());

    protected static Logger logger = LoggerFactory.getLogger(CryptoAesUtils.class);

    /**
     * 参数解密
     * @throws IOException
     */
    public static String decryptParameter(String value){
        try {
            if(StringUtils.isNotBlank(value)) {
                value = URLDecoder.decode(value, "UTF-8");
                value = value.replace(" ", "+");
                value = decrypt(value);
                if(value.indexOf(regex) > -1) {
                    value = value.split(regex)[0];
                }
            }
            return value;
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 加密
     * @return
     * @throws IOException
     */
    public static String encryptBase64(String data) throws IOException {
        return aes.encryptBase64(data);
    }

    /**
     * 加密
     * @return
     * @throws IOException
     */
    public static String encryptBase64(Object data) throws IOException {
        return aes.encryptBase64(JSONObject.toJSONString(data));
    }

    /**
     * 解密
     * @param encrypt
     * @return
     * @throws IOException
     */
    public static String decrypt(String encrypt) throws IOException {
        return aes.decryptStr(encrypt);
    }


    /**
     * URL参数加密
     * @param data
     * @return
     */
    public static String encryptURLParmas(String data){
        String ciphertext = aes.encryptBase64(data);
        // url非法参数字符处理(+)
        ciphertext = ciphertext.replaceAll("\\+","%2B");
        // url非法参数字符处理(\r\n)
        ciphertext = ciphertext.replaceAll(" ","%20");
        return ciphertext;
    }


    /**
     * URL参数解密
     * @param encrypt
     * @return
     */
    public static String decryptURLParmas(String encrypt){
        encrypt = encrypt.replaceAll("%2B", "\\+");
        encrypt = encrypt.replaceAll("%20", " ");
        return aes.decryptStr(encrypt);
    }


    public static void main(String[] args) {
        String s = decryptParameter("fEoZgzfFlgRTF8fn_28pgL76Zxcp7keKd_evh_luasrPhWFqWsnG4-PcKkhQJiB9dsMvQB7boFwz-4TTmaGwDLNCbx9z7vdca4Gd0a3TKfjKclzABzkJ-yeBI_kwq081mJfPfWmK_7pgKoGNo_MKT2x3nR9acAU9c6Uc7lGCSPE");
        System.out.println(s);
    }
}
